package com.example.demo.service.exam.impl;

import com.example.demo.domain.SingleClause;
import com.example.demo.domain.exam.ExamPaperQuestionLanguage;
import com.example.demo.enums.LanguageType;
import com.example.demo.exception.ExceptionUtils;
import com.example.demo.mapper.ExamPaperQuestionLanguageMapper;
import com.example.demo.service.exam.ExamPaperQuestionLanguageService;
import com.example.demo.service.impl.BaseServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ExamPaperQuestionLanguageServiceImpl extends BaseServiceImpl<ExamPaperQuestionLanguageMapper, ExamPaperQuestionLanguage> implements ExamPaperQuestionLanguageService {

    private final ExamPaperQuestionLanguageMapper mapper;

    @Override
    public void insertList(List<ExamPaperQuestionLanguage> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        list.forEach(this::valid);
        super.saveBatch(list);
    }

    @Override
    public List<ExamPaperQuestionLanguage> getByPaperId(Long paperId) {
        if (paperId == null) {
            throw ExceptionUtils.exception("试卷标识不能为空");
        }
        return super.selectList(new SingleClause("paper_id", paperId));
    }

    private void valid(ExamPaperQuestionLanguage entity) {
        Long questionId = entity.getQuestionId();
        Long paperId = entity.getPaperId();
        LanguageType type = entity.getType();
        if (questionId == null) {
            throw ExceptionUtils.exception("问题标识不能为空");
        }
        if (paperId == null) {
            throw ExceptionUtils.exception("试卷标识不能为空");
        }
        if (type == null) {
            throw ExceptionUtils.exception("语言类型不能为空");

        }
    }

}
